//get state-level enrollment-weighted average costs

//location and classifications
forval y = 2001 / 2005{
	clear all
	import delimited "$data/IPEDS/location/hd`y'_data_stata.csv", clear
	keep unitid fips sector iclevel control carnegie
	gen year = `y'
	save "$temp/iped_location_`y'", replace
}

//enrollment
forval y = 2001/2005{
	import delimited "$data/IPEDS/enrollment/ef`y'a_data_stata.csv", clear
	keep if line == 8 //full time undergrads
	gen total_enroll = efrace15 + efrace16
	keep unitid total
	gen year = `y'
	save "$temp/iped_enroll_`y'", replace
}

//sat/act, admissions
forval y = 2001/2005{
	import delimited "$data/IPEDS/inst/ic2001_data_stata.csv", clear
	
	if `y'==2001{
		keep unitid satmt25 satmt75 actmt25 actmt75
	}
	else{
		keep unitid satmt25 satmt75 actmt25 actmt75 applcnm applcnw admssnm admssnw
	}
	gen year = `y'
	save "$temp/iped_admission_`y'", replace
}

//instructor salaries
forval y = 2001/2005{
	import delimited "$data/IPEDS/salary/sal`y'_a_data_stata.csv", clear
	
	if `y' == 2001{
		keep if contract == 1
		keep if arank == 1 | arank == 8
		collapse (mean) saverage, by(unitid)
		ren saverage avg_sal
	}
	else{
		keep if contract == 1
		keep if arank == 1
		ren avesalt avg_sal
		keep unitid avg_sal
	}
	
	gen year = `y'
	save "$temp/iped_sal_`y'", replace
}

//number of instructors
forval y = 2001/2005{
	import delimited "$data/IPEDS/staff/s`y'_f_data_stata.csv", clear
	keep if arank==22
	gen total_staff = staff15 + staff16
	keep unitid total
	gen year = `y'
	save "$temp/iped_staff_`y'", replace
}


//student charges
forval y = 2001/2005{
	clear all
	import delimited "$data/IPEDS/charges/ic`y'_ay_data_stata.csv", clear
	
	//tuition, books, room and board
	keep unitid chg2ay3 chg3ay3 chg4ay3 chg5ay3
	ren chg2 tuition_instate
	ren chg3 tuition_outstate
	ren chg4 books
	ren chg5 roomboard
	gen year = `y'
	save "$temp/iped_charges_`y'", replace
}

//add all together
forval y = 2001/2005{
	use "$temp/iped_location_`y'", clear
	merge 1:1 unitid using "$temp/iped_enroll_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_admission_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_sal_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_staff_`y'", keep(1 3) nogen
	merge 1:1 unitid using "$temp/iped_charges_`y'", keep(1 3) nogen
	save "$temp/iped_all_`y'", replace
}

clear
forval y = 2001/2005{
	append using "$temp/iped_all_`y'"
}
save "$temp/iped_all", replace

//define college types and report average costs by types
use "$temp/iped_all", clear
drop if total_enroll == 0 | total_enroll == . //weighting by enrollment anyhow, so basically have to drop these
//do "$dofiles/99_ipeds_labels"
gen admit_rate = (admssnm + admssnw) / (applcnm + applcnw) 
replace satmt25 = 0 if satmt25==.
replace actmt25 = 0 if actmt25==.

//categories, based on Kennan (2020)
gen cat = .
replace cat = 1 if sector == 4 | sector == 7 //community college (public 2 year or below)
replace cat = 2 if sector==5 | sector == 6 | sector == 8 | sector == 9 //private two-year
replace cat = 3 if sector == 1 //other public universities

//categorize private schools based on selectivity. Want either sat25>=380 or act25>=16
replace cat = 4 if sector == 2 & (satmt25>=380 | actmt25>=16 | admit_rate<0.5)  //non-profit private schools that pass certain criteria
replace cat = 5 if cat == . //other: low-quality private schools

//save ipeds classifications
preserve
keep unitid cat fips
ren fips stfips
duplicates drop unitid, force
save "$temp/ipeds_categories", replace
restore

//in-state and out-state costs
drop if tuition_instate == . | tuition_outstate == .
gen cost_instate = tuition_instate + books + roomboard
gen cost_outstate = tuition_outstate + books + roomboard
drop tuition* books roomboard

//deflate
merge m:1 year using "$data/GDP/gdp_pce_deflator", keep(match) nogen
replace cost_instate = cost_instate/deflator * 100
replace cost_outstate = cost_outstate/deflator * 100

//report
su cost* [w = total_enroll] if cat == 1
su cost* [w = total_enroll] if cat == 2
su cost* [w = total_enroll] if cat == 3
su cost* [w = total_enroll] if cat == 4
su cost* [w = total_enroll] if cat == 5
save "$temp/ipeds_processed", replace





